<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEventGiftLessonsTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
        Schema::create('gift_lesson_students', function (Blueprint $table) {
            $table->timestamp('updated_at')->useCurrent();
            $table->timestamp('created_at');

            $table->integer('student_id')->comment('用户ID');
            $table->integer('lesson_count')->default(0)->comment('累计上课次数');
            $table->integer('last_active_lesson_count')->default(0)->comment('最后一次触发赠送上课次数');
            $table->integer('share_count')->default(0)->comment('赠送权限次数');
            $table->integer('accepted_count')->default(0)->comment('赠送已被领取次数');
            $table->integer('total_gift')->default(0)->comment('累积获得课时数');
            $table->tinyInteger('status')->default(0)->comment('特权状态：0：不可用，1：可用');

            $table->primary("student_id");

            $table->softDeletes();
        });
        Schema::create('gift_lesson_shares', function (Blueprint $table) {
            $table->timestamp('updated_at')->useCurrent();
            $table->timestamp('created_at');

            $table->increments('id');
            $table->integer('student_id')->comment('分享用户ID');
            $table->string('share_code')->coment('分享code');
            $table->integer('lesson_count')->default(0)->comment('新用户购买后获赠课时数');
            $table->timestamp('expired_at')->comment('领取过期时间');
            $table->tinyInteger('status')->comment('0：未领取，1：已领取， 2：已过期');

            $table->softDeletes();

            $table->index('student_id');
            $table->index('share_code');
        });
        Schema::create('gift_lesson_acceptances', function (Blueprint $table) {
            $table->timestamp('updated_at')->useCurrent();
            $table->timestamp('created_at');

            $table->increments('id');
            $table->integer('student_id')->comment('领取用户ID');
            $table->integer('share_id')->comment('分享记录ID');
            $table->integer('lesson_count')->comment('领取课时数');
            $table->timestamp('expired_at')->comment('触发赠送时间限制');
            $table->integer('bill_id')->nullable()->comment('触发赠送订单ID');
            $table->timestamp('gifted_at')->nullable()->comment('触发赠送时间');
            $table->tinyInteger('status')->default(0)->comment('0：已领取 1：已购买 2：已过期');

            $table->softDeletes();

            $table->index('student_id');
            $table->index('share_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
        Schema::drop('gift_lesson_students');
        Schema::drop('gift_lesson_shares');
        Schema::drop('gift_lesson_acceptances');
    }
}
